#####
## Replication File:
##
## The Relationship between Social Media Use and 
## Beliefs in Conspiracy Theories and Misinformation
#####

################################################################################

####
## Load necessary packages
## 
## Analyses conducted in RStudio 1.0.153,
## using R 3.6 (Mac OS)
####

library(stats)
library(lattice)
library(latticeExtra)
library(foreign)

# set working directory
setwd("~/Dropbox/Social Media and Conspiracy Beliefs/Data and Code/Replication")

################################################################################

####
## Figure 1
####

march2020 <- read.dta("Clean Data, March 2020.dta")

hist1 <- histogram(~as.factor(number),
          data = march2020,
          aspect = 1,
          xlab = "Number of Conspiracy Beliefs",
          )

hist2 <- histogram(~qanonft,
          data = march2020,
          aspect = 1,
          xlab = "QAnon Thermometer Score"
)

tiff("figure1.tiff", width=8, height=4, units = "in", res=300, compression = 'lzw')
print(hist1, position = c(0, 0, .5, 1), more = TRUE)
print(hist2, position = c(.5, 0, 1, 1), more = FALSE)
dev.off()

################################################################################

####
## Figure 2
####

medium1 <- read.csv("Beliefs by Medium, March.csv", header = TRUE)

medium1$var <- reorder(medium1$medium, -medium1$order)

plot1 <- xyplot(as.factor(var) ~ number,
                data = medium1,
                aspect = 1.5,
                col = "black",
                pch = 16,
                xlim = c(3, 7.5),
                ylab = " ",
                main = "A. Number of Conspiracy Beliefs (0-15)",
                xlab = "Number of Beliefs",
                #par.settings = list(clip = list(panel = FALSE)), 
                panel = function(x, y, ...){
                        panel.xyplot(x, y, col=...)
                        panel.segments(medium1$lower, as.factor(medium1$var),
                                       medium1$upper, as.factor(medium1$var))
                        #panel.text(2.5, 7, label = "A)", font = 2)
                }
)


medium2 <- read.csv("Beliefs by Medium, QAnon.csv", header = TRUE)

medium2$var <- reorder(medium2$medium, -medium2$order)

plot2 <- xyplot(as.factor(var) ~ mean,
                data = medium2,
                aspect = 1.5,
                col = "black",
                pch = 16,
                xlim = c(12, 48),
                ylab = " ",
                xlab = "Mean Thermometer Score",
                main = "B. QAnon Thermometer (0-100)",
                #par.settings = list(clip = list(panel = FALSE)), 
                panel = function(x, y, ...){
                        panel.xyplot(x, y, col=...)
                        panel.segments(medium2$lower, as.factor(medium2$var),
                                       medium2$upper, as.factor(medium2$var))
                        #panel.text(2.5, 7, label = "A)", font = 2)
                }
)

tiff("figure2.tiff", width=8, height=5, units = "in", res=300, compression = 'lzw')
print(plot1, position = c(0, 0, .5, 1), more = TRUE)
print(plot2, position = c(.5, 0, 1, 1), more = FALSE)
dev.off()

################################################################################

####
## Figure 3
####

corrs1 <- read.csv("Correlations, March.csv", header = TRUE)

corrs1$var <- reorder(corrs1$website, -corrs1$order)

tiff("figure3.tiff", width=4, height=5, units = "in", res=300, compression = 'lzw')
xyplot(scale ~ estimate,
       data = corrs1,
       aspect = 1.5,
       groups = group,
       col = "black",
       pch = c(16, 17),
       xlim = c(0.05, .5),
       ylab = " ",
       xlab = "Correlation Coefficient",
       key = list(columns=1,points=list(pch=c(16,17)), 
                text=list(c("Number of Conspiracy Beliefs", "QAnon Thermometer"))),
       scales = list(y = list(labels = c("", "4chan/8chan", "Youtube", "Reddit",
                                         "Instagram", "Twitter", "Facebook"))),
       panel = function(x, y, ...){
               panel.xyplot(x, y, col=...)
               panel.segments(corrs1$lower, corrs1$scale,
                              corrs1$upper, corrs1$scale)
       }
)
dev.off()

################################################################################

####
## Figure 4
####

mfx1 <- read.csv("Marginal Effects, March.csv", header = TRUE)

mplot1 <- xyplot(estimate ~ scale,
                 data = mfx1,
                 aspect = 1,
                 group = group,
                 lty = c(1, 2, 2),
                 type = "l",
                 col = "black",
                 ylab = "Marginal Effect of Social Media Use",
                 xlab = "Conspiracy Thinking",
                 main = "A. Number of Conspiracy Beliefs (0-15)",
                 panel = function(x, y, ...){
                         panel.xyplot(x, y, col=...)
                         panel.abline(h=0, col="dark gray")
                 }
)

mfx2 <- read.csv("Marginal Effects, QAnon.csv", header = TRUE)

mplot2 <- xyplot(estimate ~ scale,
                 data = mfx2,
                 aspect = 1,
                 group = group,
                 lty = c(1, 2, 2),
                 type = "l",
                 col = "black",
                 ylab = "Marginal Effect of Social Media Use",
                 xlab = "Conspiracy Thinking",
                 main = "B. QAnon Thermometer (0-100)",
                 panel = function(x, y, ...){
                         panel.xyplot(x, y, col=...)
                         panel.abline(h=0, col="dark gray")
                 }
)

tiff("figure4.tiff", width=9, height=4.5, units = "in", res=300, compression = 'lzw')
print(mplot1, position = c(0, 0, .5, 1), more = TRUE)
print(mplot2, position = c(.5, 0, 1, 1), more = FALSE)
dev.off()

################################################################################

####
## Figure 5
####

june2020 <- read.dta("Clean Data, June 2020.dta")

hist3 <- histogram(~as.factor(gennumber),
          data = june2020,
          aspect = 1,
          main = "A. Non-COVID Conspiracy Theories",
          xlab = "Number of Conspiracy Beliefs"
)

hist4 <- histogram(~as.factor(covnumber),
          data = june2020,
          aspect = 1,
          main = "B. COVID Conspiracy Theories",
          xlab = "Number of Beliefs"
)

hist5 <- histogram(~as.factor(misnumber),
          data = june2020,
          aspect = 1,
          main = "C. COVID Misinformation",
          xlab = "Number of Beliefs"
)

tiff("figure5.tiff", width=8, height=8, units = "in", res=300, compression = 'lzw')
print(hist3, position = c(0, .5, .5, 1), more = TRUE)
print(hist4, position = c(.5, .5, 1, 1), more = TRUE)
print(hist5, position = c(.25, 0, .75, .5), more = FALSE)
dev.off()

################################################################################

####
## Figure 6
####

medium2 <- read.csv("Beliefs by Medium, June.csv", header = TRUE)

medium2$var <- reorder(medium2$medium, -medium2$order)

medium2$cond[medium2$belief=="Non-COVID Conspiracies"] <- 3
medium2$cond[medium2$belief=="COVID Conspiracies"] <- 1
medium2$cond[medium2$belief=="COVID Misinformation"] <- 2

tiff("figure6.tiff", width=10, height=5.5, units = "in", res=300, compression = 'lzw')
xyplot(as.factor(var) ~ number | belief,
       data = medium2,
       aspect = 1.5,
       col = "black",
       pch = 16,
       xlim = c(0.2, 2.5),
       ylab = " ",
       xlab = "Number of Beliefs",
       strip=strip.custom(factor.levels=c("Non-COVID Conspiracy Beliefs", 
                                          "COVID Conspiracy Theories",  
                                          "COVID Misinformation")),
       scond = medium2$cond,
       panel = function(x, y, scond, ...){
         pnl = panel.number()
         panel.xyplot(x, y, col=...)
         panel.segments(medium2$lower[scond==pnl], as.factor(medium2$var),
                        medium2$upper[scond==pnl], as.factor(medium2$var))
       }
)
dev.off()

################################################################################

####
## Figure 7
####

corrs2 <- read.csv("Correlations, June.csv", header = TRUE)

corrs2$var <- reorder(corrs2$website, -corrs2$order)

corrs2$cond[corrs2$belief=="Non-COVID Conspiracies"] <- 3
corrs2$cond[corrs2$belief=="COVID Conspiracies"] <- 1
corrs2$cond[corrs2$belief=="COVID Misinformation"] <- 2

tiff("figure7.tiff", width=10, height=5.5, units = "in", res=300, compression = 'lzw')
xyplot(as.factor(var) ~ estimate | belief,
       data = corrs2,
       aspect = 1.5,
       col = "black",
       pch = 16,
       xlim = c(-0.05, .5),
       ylab = " ",
       xlab = "Correlation with Number of Beliefs",
       strip=strip.custom(factor.levels=c("Non-COVID Conspiracy Beliefs", 
                                          "COVID Conspiracy Theories",  
                                          "COVID Misinformation")),
       scond = corrs2$cond,
       panel = function(x, y, scond, ...){
               pnl = panel.number()
               panel.xyplot(x, y, col=...)
               panel.segments(corrs2$lower[scond==pnl], as.factor(corrs2$var),
                              corrs2$upper[scond==pnl], as.factor(corrs2$var))
       }
)
dev.off()

################################################################################

####
## Figure 8
####

mfx3 <- read.csv("Marginal Effects, June.csv", header = TRUE)

tiff("figure8.tiff", width=9, height=4, units = "in", res=300, compression = 'lzw')
xyplot(estimate ~ scale | belief,
       data = mfx3,
       aspect = 1,
       group = group,
       lty = c(1, 2, 2),
       type = "l",
       col = "black",
       ylab = "Marginal Effect of Social Media Use",
       xlab = "Conspiracy Thinking",
       layout = c(3, 1),
       panel = function(x, y, ...){
         panel.xyplot(x, y, col=...)
         panel.abline(h=0, col="dark gray")
       }
)
dev.off()

################################################################################
####
## Figure A1
####

mfx3 <- read.csv("Marginal Effects, March Tobit.csv", header = TRUE)

mplot3 <- xyplot(estimate ~ scale,
                 data = mfx3,
                 aspect = 1,
                 group = group,
                 lty = c(1, 2, 2),
                 type = "l",
                 col = "black",
                 ylab = "Marginal Effect of Social Media Use",
                 xlab = "Conspiracy Thinking",
                 main = "A. Number of Conspiracy Beliefs (0-15)",
                 panel = function(x, y, ...){
                         panel.xyplot(x, y, col=...)
                         panel.abline(h=0, col="dark gray")
                 }
)

mfx4 <- read.csv("Marginal Effects, QAnon Tobit.csv", header = TRUE)

mplot4 <- xyplot(estimate ~ scale,
                 data = mfx4,
                 aspect = 1,
                 group = group,
                 lty = c(1, 2, 2),
                 type = "l",
                 col = "black",
                 ylab = "Marginal Effect of Social Media Use",
                 xlab = "Conspiracy Thinking",
                 main = "B. QAnon Thermometer (0-100)",
                 panel = function(x, y, ...){
                         panel.xyplot(x, y, col=...)
                         panel.abline(h=0, col="dark gray")
                 }
)

tiff("figureA1.tiff", width=9, height=4, units = "in", res=300, compression = 'lzw')
print(mplot3, position = c(0, 0, .5, 1), more = TRUE)
print(mplot4, position = c(.5, 0, 1, 1), more = FALSE)
dev.off()

################################################################################
####
## Figure A2
####

mfx5 <- read.csv("Marginal Effects, June Tobit.csv", header = TRUE)

tiff("figureA2.tiff", width=9, height=4, units = "in", res=300, compression = 'lzw')
xyplot(estimate ~ scale | belief,
       data = mfx5,
       aspect = 1,
       group = group,
       lty = c(1, 2, 2),
       type = "l",
       col = "black",
       ylab = "Marginal Effect of Social Media Use",
       xlab = "Conspiracy Thinking",
       layout = c(3, 1),
       panel = function(x, y, ...){
               panel.xyplot(x, y, col=...)
               panel.abline(h=0, col="dark gray")
       }
)
dev.off()

################################################################################
####
## Figure A3
####

mfx6 <- read.csv("Marginal Effects, October.csv", header = TRUE)

mplot5 <- xyplot(estimate ~ scale,
                 data = mfx6,
                 aspect = 1,
                 group = group,
                 lty = c(1, 2, 2),
                 type = "l",
                 col = "black",
                 ylab = "Marginal Effect of Social Media Use",
                 xlab = "Conspiracy Thinking",
                 main = "A. Number of Conspiracy Beliefs (0-9)",
                 panel = function(x, y, ...){
                         panel.xyplot(x, y, col=...)
                         panel.abline(h=0, col="dark gray")
                 }
)

mfx7 <- read.csv("Marginal Effects, QAnon October.csv", header = TRUE)

mplot6 <- xyplot(estimate ~ scale,
                 data = mfx7,
                 aspect = 1,
                 group = group,
                 lty = c(1, 2, 2),
                 type = "l",
                 col = "black",
                 ylab = "Marginal Effect of Social Media Use",
                 xlab = "Conspiracy Thinking",
                 main = "B. QAnon Thermometer (0-100)",
                 panel = function(x, y, ...){
                         panel.xyplot(x, y, col=...)
                         panel.abline(h=0, col="dark gray")
                 }
)

tiff("figureA3.tiff", width=9, height=4.5, units = "in", res=300, compression = 'lzw')
print(mplot5, position = c(0, 0, .5, 1), more = TRUE)
print(mplot6, position = c(.5, 0, 1, 1), more = FALSE)
dev.off()

################################################################################
####
## Figure A4
####

mfx8 <- read.csv("Marginal Effects, Florida.csv", header = TRUE)

tiff("figureA4.tiff", width=9, height=4, units = "in", res=300, compression = 'lzw')
xyplot(estimate ~ scale | belief,
       data = mfx8,
       aspect = 1,
       group = group,
       lty = c(1, 2, 2),
       type = "l",
       col = "black",
       ylab = "Marginal Effect of Social Media Use",
       xlab = "Conspiracy Thinking",
       scales = list(y = list(relation="free")),
       layout = c(3, 1),
       panel = function(x, y, ...){
               panel.xyplot(x, y, col=...)
               panel.abline(h=0, col="dark gray")
       }
)
dev.off()

